package com.pozu.new_retail.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.map.ObjectMapper;

/***
 * 工具类
 * @author chao
 * @date 2018年4月20日
 */
public class Tools {
	
	
	@SuppressWarnings("unchecked")
	public static Map<String,Object> getRequestData(HttpServletRequest request) throws IOException {
		BufferedReader bufferReader = request.getReader();
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = bufferReader.readLine()) != null) {
			sb.append(line.trim());
		}
		//若body中传送的参数加密,则再此处进行解密
		LogUtils.INFO.info("请求Body参数：{}", sb.toString());
		
		return new ObjectMapper().readValue(sb.toString(), Map.class);
	}
	
	/**
	 * 返回一个对象
	 * @param request
	 * @param t
	 * @return
	 * @throws IOException
	 */
	public static <T> T getRequestObject(HttpServletRequest request,Class<T> t) throws IOException {
		
		BufferedReader bufferReader = request.getReader();
		StringBuilder sb = new StringBuilder();
		String line = null;
		while ((line = bufferReader.readLine()) != null) {
			sb.append(line.trim());
		}
		//若body中传送的参数加密,则再此处进行解密
		LogUtils.INFO.info("请求Body参数：{}", sb.toString());
		
		return new ObjectMapper().readValue(sb.toString(), t);
	}

	/**
	 * 获取随机字符串
	 * @return
	 */
	public static String getRandomCode(int length) {
		char[] arr = {  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
					'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 
					'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
		
		StringBuffer sb = new StringBuffer();
		for(int i=0; i<length; i++) {
			int index = (int)(Math.random()*arr.length);
			sb.append(arr[index]);
		}
		
		return sb.toString();
		
	}
	
	/**
	 * 获取当前时间
	 * @param format
	 * @return
	 */
	public static String getDateStr(String format){
		return new SimpleDateFormat(format).format(new Date());
	}
	
	/**
	 * 获取随机字符串
	 * @return
	 */
	public static String getRandomNumber(int length) {
		char[] arr = {  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
		
		StringBuffer sb = new StringBuffer();
		for(int i=0; i<length; i++) {
			int index = (int)(Math.random()*arr.length);
			sb.append(arr[index]);
		}
		
		return sb.toString();	
	}
	
	/**
	 * httpGet请求
	 * @param url
	 * @param headMap
	 * @return
	 */
	public static String httpGet(String url,Map<String,String> headMap){  
		String result = null;
        try {
        	CloseableHttpClient httpclient = HttpClients.createDefault();
        	HttpGet httpGet = new HttpGet(url);
        	if(headMap!=null) {
	            for(String key:headMap.keySet()){
	            	httpGet.setHeader(key, headMap.get(key));
	            }
        	}
            HttpResponse response = httpclient.execute(httpGet);  
            if(response != null){  
                HttpEntity resEntity = response.getEntity();  
                if(resEntity != null){  
                    result = EntityUtils.toString(resEntity,"utf-8");  
                }  
            } 
        }catch(Exception e){
        	e.printStackTrace();
        }
        return result;  
    }
	
	/***
	 * 获取double值
	 * @param number
	 * @return
	 */
	public static double getDoubleValue(double number) {
		
		DecimalFormat dFormat = new DecimalFormat("0.00");
		
		return Double.parseDouble(dFormat.format(number)) == 0.0 ? 0.00 : Double.parseDouble(dFormat.format(number));
	}

}
